package at.tuwien.seminar.scrum1;

import at.tuwien.seminar.scrum1.model.Reservation;
import org.apache.wicket.Application;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.protocol.http.WebApplication;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import at.tuwien.seminar.scrum1.hibernate.HibernateUtil;
import at.tuwien.seminar.scrum1.model.Customer;
import at.tuwien.seminar.scrum1.model.Room;
import at.tuwien.seminar.scrum1.proxies.CustomerProxy;
import at.tuwien.seminar.scrum1.resources.ResourceLocator;
import at.tuwien.seminar.scrum1.view.titlepage.TitlePage;

import java.util.ArrayList;
import java.util.Date;

/**
 * Main Wicket Page. No Need to Change
 * 
 * URL :=> http://localhost:8080/Scrum1/
 * 
 * @author KimmelF
 * 
 */
public class Scrum1WebApplication extends WebApplication {

	public Scrum1WebApplication() {
		fillTestData();
	}
	
	@Override
	protected void init() {
		super.init();

		mountSharedResource("/images/header-bg.gif", new ResourceReference(ResourceLocator.class, "header-bg.gif").getSharedResourceKey());
		mountSharedResource("/images/header-selected-bg.gif", new ResourceReference(ResourceLocator.class, "header-selected-bg.gif").getSharedResourceKey());
		mountSharedResource("/images/sort.gif", new ResourceReference(ResourceLocator.class, "sort.gif").getSharedResourceKey());
		mountSharedResource("/images/asc.gif", new ResourceReference(ResourceLocator.class, "asc.gif").getSharedResourceKey());
		mountSharedResource("/images/desc.gif", new ResourceReference(ResourceLocator.class, "desc.gif").getSharedResourceKey());

        mountSharedResource("/images/bottom.jpg", new ResourceReference(ResourceLocator.class, "bottom.jpg").getSharedResourceKey());
        mountSharedResource("/images/bottom2.jpg", new ResourceReference(ResourceLocator.class, "bottom2.jpg").getSharedResourceKey());
        mountSharedResource("/images/box.gif", new ResourceReference(ResourceLocator.class, "box.gif").getSharedResourceKey());
        mountSharedResource("/images/box.jpg", new ResourceReference(ResourceLocator.class, "box.jpg").getSharedResourceKey());
        mountSharedResource("/images/entry.gif", new ResourceReference(ResourceLocator.class, "entry.gif").getSharedResourceKey());
        mountSharedResource("/images/image.gif", new ResourceReference(ResourceLocator.class, "image.jpg").getSharedResourceKey());
        mountSharedResource("/images/menu.jpg", new ResourceReference(ResourceLocator.class, "menu.jpg").getSharedResourceKey());
        mountSharedResource("/images/menubr.gif", new ResourceReference(ResourceLocator.class, "menubr.gif").getSharedResourceKey());
        mountSharedResource("/images/post.gif", new ResourceReference(ResourceLocator.class, "post.gif").getSharedResourceKey());
        mountSharedResource("/images/posth2.gif", new ResourceReference(ResourceLocator.class, "posth2.gif").getSharedResourceKey());
        mountSharedResource("/images/sidebarh2.gif", new ResourceReference(ResourceLocator.class, "sidebarh2.gif").getSharedResourceKey());


		mountSharedResource("/css/style.css", new ResourceReference(ResourceLocator.class, "style.css").getSharedResourceKey());
	};
	
	@Override
	public Class<TitlePage> getHomePage() {
		return TitlePage.class;
	}

	@Override
	public String getConfigurationType() {
		return Application.DEPLOYMENT;
	}

	
	private void fillTestData() {
		/** Getting the EntityManager */
		EntityManager em = HibernateUtil.getEntityManagerFactory().createEntityManager();
		
		/** Starting the Transaction */
		EntityTransaction tx = em.getTransaction();
		
		tx.begin();
		
		Room room = new Room();
		room.setName("room 1");
		room.setMaxNumberOfPersons(2);
		room.setPriceSingleRoom(100.0);
		room.setPriceDoubleRoom(150.0);
		em.merge(room);
		
		room = new Room();
		room.setName("room 2");
		room.setMaxNumberOfPersons(2);
		room.setPriceSingleRoom(200.0);
		room.setPriceDoubleRoom(300.0);
		em.merge(room);
		
		room = new Room();
		room.setName("room 3");
		room.setMaxNumberOfPersons(2);
		room.setPriceSingleRoom(1000.0);
		room.setPriceDoubleRoom(1500.0);
		room.setPriceSingleRoomWithTwoChild(1900.0);
		room.setPriceTripleRoom(1900.0);
		room = em.merge(room);
		
		Customer customer = new Customer();
		customer.setFirstName("Max");
		customer.setLastName("Mustermann");
		customer.setCompanyName("TU Wien");
		customer.setDiscount(20.0);
		customer.setEmail("max@gmail.com");
		customer.setFax("+431234567");
		customer.setNotes("some notes");
		customer.setPhone("0664 34343");
		customer.setWeburl("");
		em.merge(customer);
		
		customer = new Customer();
		customer.setFirstName("John");
		customer.setLastName("Doe");
		customer.setCompanyName("TU Wien");
		customer.setDiscount(30.0);
		customer.setEmail("john.doe@mailinator.com");
		customer.setFax("01 6008856");
		customer.setNotes("");
		customer.setPhone("0676 1234567");
		customer.setWeburl("http://www.tiss.tuwien.ac.at");
		customer = em.merge(customer);

        Reservation reservation = new Reservation();

        reservation.setRoom(room);
        reservation.setCustomers(new ArrayList<Customer>());
        reservation.getCustomers().add(customer);
        reservation.setStartDate(new Date());
        reservation.setEndDate(new Date());
        reservation.setPrice(100.0);

        em.merge(reservation);
        
        reservation = new Reservation();

        reservation.setRoom(room);
        reservation.setCustomers(new ArrayList<Customer>());
        reservation.getCustomers().add(CustomerProxy.getInstance().getAll().get(1));
        reservation.setStartDate(new Date(new Date().getTime()+100000000));
        reservation.setEndDate(new Date(new Date().getTime()+250000000));
        reservation.setPrice(200.0);
        em.merge(reservation);      
        
		/** Commiting the changes */
		tx.commit();
	}
}
